package com.lucker.controller.caipiao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lucker.common.util.CaiPiaoType;
import com.lucker.common.util.DateTimeUtil;
import com.lucker.controller.BaseController;
import com.lucker.ds.model.CaiPiao;
import com.lucker.ds.model.CaiPiaoOpenTime;
import com.lucker.ds.model.EvenyDayOpenTime;
import com.lucker.service.caipiao.CaiPiaoOpenTimeService;
import com.lucker.service.caipiao.CaiPiaoService;
import com.lucker.service.evenydayopentime.EvenyDayOpenTimeService;

@Controller
@RequestMapping("evenydayopentime")
public class EvenyDayOpenTimeController extends BaseController {
	@Autowired
	EvenyDayOpenTimeService evenyDayOpenTimeService;
	@Autowired
	CaiPiaoService caiPiaoService;
	@Autowired
	CaiPiaoOpenTimeService caiPiaoOpenTimeService;
	
	@RequestMapping(value="/init11",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> initEvenyDayOpenTime(HttpServletRequest req){
		getLoger().error("执行了初始化每日开奖号码的方法.......................");
		Date cdate = new Date() ;
		try {
			
			Map<String,Object> param = readParamToMap(req);
			if(param != null) {
				String currTime = (String)param.get("currTime");
				if(StringUtils.isBlank(currTime)) {
					cdate = new Date();
				}else {
					cdate = DateTimeUtil.getDateByString(currTime, DateTimeUtil.FORMAT_LONGTIME);
				}
			}		
			@SuppressWarnings("unused")
			String stortRiQi = DateTimeUtil.getDate(cdate, "yyMMdd");
			String time_fix = DateTimeUtil.getDate(cdate, "yyyy-MM-dd");
			//初始化11选5的每日开奖时间
			CaiPiao _caipiao = new CaiPiao();
			_caipiao.setCaiTypeId(CaiPiaoType.ELEVEN_SELECTD_FIVE);
			List<CaiPiao> _list = caiPiaoService.queryAll(_caipiao);
			
			
			
			for(CaiPiao p : _list) {
				Long _caiid = p.getCaiId().longValue();
				CaiPiaoOpenTime _time = new CaiPiaoOpenTime();
				_time.setCaiId(_caiid);
				List<CaiPiaoOpenTime> _list2 = caiPiaoOpenTimeService.queryAll(_time);
				for(CaiPiaoOpenTime t : _list2) {
					//
					getLoger().error("初始化开奖时间，期:" + stortRiQi + t.getCaipiaoIssueno());
					EvenyDayOpenTime insertEvenyDayOpenTime = new EvenyDayOpenTime();
					insertEvenyDayOpenTime.setIssuenoPrefix(stortRiQi);
					insertEvenyDayOpenTime.setIssueno(stortRiQi + t.getCaipiaoIssueno());
					insertEvenyDayOpenTime.setCaiId(_caiid);
					List<EvenyDayOpenTime> lists = evenyDayOpenTimeService.queryAll(insertEvenyDayOpenTime);
					if(CollectionUtils.isEmpty(lists)) {
						String dateTimeString = time_fix + " " + formatTimeString(t.getOpenTime());
						Date openTime = null;
						try {
							openTime = DateTimeUtil.getDateByString(dateTimeString, "yyyy-MM-dd HH:mm:ss");
						}catch (Exception e) {}
						
						if(openTime.getTime() > cdate.getTime()) {
							insertEvenyDayOpenTime.setOpenDate(openTime);
						}else {
							openTime = DateTimeUtil.addDay(openTime, 1);
							insertEvenyDayOpenTime.setOpenDate(openTime);
						}
						evenyDayOpenTimeService.add(insertEvenyDayOpenTime, false);	
					}
						
				}
			}
			
			cdate = DateTimeUtil.addDay(cdate, 1);
			stortRiQi = DateTimeUtil.getDate(cdate, "yyMMdd");
			time_fix = DateTimeUtil.getDate(cdate, "yyyy-MM-dd");
			
			for(CaiPiao p : _list) {
				Long _caiid = p.getCaiId().longValue();
				CaiPiaoOpenTime _time = new CaiPiaoOpenTime();
				_time.setCaiId(_caiid);
				List<CaiPiaoOpenTime> _list2 = caiPiaoOpenTimeService.queryAll(_time);
				for(CaiPiaoOpenTime t : _list2) {
					//
					getLoger().error("初始化开奖时间，期:" + stortRiQi + t.getCaipiaoIssueno());
					EvenyDayOpenTime insertEvenyDayOpenTime = new EvenyDayOpenTime();
					insertEvenyDayOpenTime.setIssuenoPrefix(stortRiQi);
					insertEvenyDayOpenTime.setIssueno(stortRiQi + t.getCaipiaoIssueno());
					insertEvenyDayOpenTime.setCaiId(_caiid);
					List<EvenyDayOpenTime> lists = evenyDayOpenTimeService.queryAll(insertEvenyDayOpenTime);
					if(CollectionUtils.isEmpty(lists)) {
						String dateTimeString = time_fix + " " + formatTimeString(t.getOpenTime());
						Date openTime = null;
						try {
							openTime = DateTimeUtil.getDateByString(dateTimeString, "yyyy-MM-dd HH:mm:ss");
						}catch (Exception e) {}
						if(openTime.getTime() > cdate.getTime()) {
							insertEvenyDayOpenTime.setOpenDate(openTime);
						}else {
							openTime = DateTimeUtil.addDay(openTime, 1);
							insertEvenyDayOpenTime.setOpenDate(openTime);
						}
						evenyDayOpenTimeService.add(insertEvenyDayOpenTime, false);	
					}
				}
			}
			return null;
		} catch (Exception e) {
			getLoger().error("初始化每日开奖错误 {},{}",e.getMessage(),e);
			return null;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public String formatTimeString(String shortTimeString) {
		
		if(StringUtils.isNotBlank(shortTimeString) && shortTimeString.indexOf(":") > 0) {
			if(StringUtils.substring(shortTimeString,0, shortTimeString.indexOf(":")).length() == 1 ) {
				return "0" + shortTimeString;
			}else {
				return shortTimeString;
			}
		}
		return null;
	}

}
